from First.DataAccess import utils
from First import views
from First import models
import requests

def zhuanhualoudou():
    def hanneihuoqushuju():
        access_token, appkey = utils.huoquyonghujiapi()
        method = 'conversion/conversion/getList'
        url = 'https://openapi.baidu.com/rest/2.0/mtj/svc/app/getDataByKey?access_token={access_token}&key={appkey}&method={method}'.format(
            access_token=access_token, appkey=appkey, method=method)
        return request_data(url)

    str1 = hanneihuoqushuju()
    # print(str1)
    if 'error_code' in str1:
        utils.gengxinyonghujiapi()
        str1 = hanneihuoqushuju()
        if 'error_code' in str1:
            views.hanshurwzanting('rwzhuanhualoudou')
    if 'error_code' not in str1:
        dataqingxi = {}
        dataqingxi['result'] = []
        for num1 in range(0, len(str1['result'])):
            letlist = []
            for num2 in range(0, len(str1['result'][num1]['stepList'])):
                letlist.append({'id':str1['result'][num1]['stepList'][num2]['id'],'name':str1['result'][num1]['stepList'][num2]['name'],'order':str1['result'][num1]['stepList'][num2]['order']})
            if(len(str1['result'][num1]['channelList'])):
                dataqingxi['result'].append({'id': str1['result'][num1]['id'], 'name': str1['result'][num1]['name'], 'stepList': letlist, 'qudao': 1})
            else:
                dataqingxi['result'].append({'id':str1['result'][num1]['id'],'name':str1['result'][num1]['name'],'stepList':letlist})
        return dataqingxi

def qudaozhuanhualoudou(start_date,end_date):
    def hanneihuoqushuju(conversionid,start_date, end_date):
        access_token, appkey = utils.huoquyonghujiapi()
        method = 'conversion/channelconversion/f'
        metrics = 'conversion_ratio&nature=1&conversionId='+conversionid+'&type=funnel'
        url = 'https://openapi.baidu.com/rest/2.0/mtj/svc/app/getDataByKey?access_token={access_token}&key={appkey}&method={method}&start-date={start_date}&end-date={end_date}&metrics={metrics}'.format(
            access_token=access_token, appkey=appkey, method=method, start_date=start_date, end_date=end_date,
            metrics=metrics)
        return request_data(url)

    dataqingxi = {}
    dataqingxi['result'] = []
    ulist = models.Zhuanhuafenxi.objects.last()
    for num1 in range(0, len(ulist.zhuanhualoudou['result'])):
        if ("qudao" in ulist.zhuanhualoudou['result'][num1]):
            letlist = []
            str1 = hanneihuoqushuju(ulist.zhuanhualoudou['result'][num1]['id'], start_date, end_date)
            # print(str1)
            if 'error_code' in str1:
                utils.gengxinyonghujiapi()
                str1 = hanneihuoqushuju(ulist.zhuanhualoudou['result'][num1]['id'], start_date, end_date)
                if 'error_code' in str1:
                    views.hanshurwzanting('rwqudaozhuanhualoudou')
            if 'error_code' not in str1:
                for num2 in range(0, len(ulist.zhuanhualoudou['result'][num1]['stepList'])):
                    letlist.append({'id': ulist.zhuanhualoudou['result'][num1]['stepList'][num2]['id'],
                                    'name': ulist.zhuanhualoudou['result'][num1]['stepList'][num2]['name'],
                                    'value': 0 if str1['result']['items'][1][num2][0] == "--" else int(str1['result']['items'][1][num2][0]),
                                    'order': ulist.zhuanhualoudou['result'][num1]['stepList'][num2]['order']})
                dataqingxi['result'].append(
                    {'id': ulist.zhuanhualoudou['result'][num1]['id'],
                     'name': ulist.zhuanhualoudou['result'][num1]['name'], 'stepList': letlist})
    return dataqingxi

def lujingzhuanhualoudou(start_date,end_date):
    def hanneihuoqushuju(conversionid,start_date, end_date):
        access_token, appkey = utils.huoquyonghujiapi()
        method = 'conversion/pathconversion/f'
        metrics = 'conversion_ratio&nature=1&conversionId='+conversionid+'&type=funnel'
        url = 'https://openapi.baidu.com/rest/2.0/mtj/svc/app/getDataByKey?access_token={access_token}&key={appkey}&method={method}&start-date={start_date}&end-date={end_date}&metrics={metrics}'.format(
            access_token=access_token, appkey=appkey, method=method, start_date=start_date, end_date=end_date,
            metrics=metrics)
        return request_data(url)

    dataqingxi = {}
    dataqingxi['result'] = []
    ulist = models.Zhuanhuafenxi.objects.last()
    for num1 in range(0, len(ulist.zhuanhualoudou['result'])):
        if ("qudao" not in ulist.zhuanhualoudou['result'][num1]):
            letlist = []
            str1 = hanneihuoqushuju(ulist.zhuanhualoudou['result'][num1]['id'], start_date, end_date)
            # print(str1)
            if 'error_code' in str1:
                utils.gengxinyonghujiapi()
                str1 = hanneihuoqushuju(ulist.zhuanhualoudou['result'][num1]['id'], start_date, end_date)
                if 'error_code' in str1:
                    views.hanshurwzanting('rwlujingzhuanhualoudou')
            if 'error_code' not in str1:
                for num2 in range(0, len(ulist.zhuanhualoudou['result'][num1]['stepList'])):
                    letlist.append({'id': ulist.zhuanhualoudou['result'][num1]['stepList'][num2]['id'],
                                    'name': ulist.zhuanhualoudou['result'][num1]['stepList'][num2]['name'],
                                    'value': 0 if str1['result']['items'][1][num2][0] == "--" else int(str1['result']['items'][1][num2][0]),
                                    'order': ulist.zhuanhualoudou['result'][num1]['stepList'][num2]['order']})
                dataqingxi['result'].append(
                    {'id': ulist.zhuanhualoudou['result'][num1]['id'],
                     'name': ulist.zhuanhualoudou['result'][num1]['name'], 'stepList': letlist})
    return dataqingxi


def request_data(url):
    req = requests.get(url, timeout=30)  # 请求连接
    req_jason = req.json()  # 获取数据
    return req_jason